
var MainScript ={
    InitDialog: function(){
        $('#dialog').dialog({
            autoOpen: false,
            width: 800,
            height:500,
            modal:true
        //            buttons: {
        //                "Chỉnh sửa": function() {
        //                    $(this).dialog("close");
        //                }
        //            }
        });
    },

    InitMessagebox: function(){
        $('#messagebox').dialog({
            autoOpen: false,
            width: 290,
            height:170,
            modal:true
        });
    },

    InitViewDetail: function(){
        $("a.detail").click(function(){
            GetOrderDetail($(this).attr('orderId'));
        });
    },

    InitDeleteOrder: function(){   
        $("a.delete").click(function(){
            var id = $(this).attr('orderId');
            $('#messagebox').html("Bạn có chắc chắn muốn xóa đơn đặt hàng có mã "+ $(this).attr('orderId'));
            $("#messagebox").dialog('open');
            $('#messagebox').dialog({
                buttons: {
                    "Xóa": function() {
                        DeleteOrder(id);
                    },
                    "Hủy bỏ": function(){
                        $(this).dialog("close");
                    }
                }
            })
        });
    },

    InitUpdateOrderState: function(){
        $("input.checked").click(function(){
            var orderId=$(this).attr("orderId");
            var check=0;
            if($(this).is(":checked")){
                check=1;
            }
            UpdateOrderState(orderId, check);
        });
    },

    InitSearch:function(){
        $("#searchByOrderDate").click(function(){
            GetOrdersPagerByDateOrder(1);
        });
        $("#searchByDeliDate").click(function(){
            GetOrdersPagerByDateDelivery(1);
        });
        $("#searchByCheck").click(function(){
            GetOrdersPagerByCheck(1);
        });
        $("#searchByEmail").click(function(){
            GetOrdersPagerByEmail(1);
        });
        $("#searchByPhone").click(function(){
            GetOrdersPagerByPhone(1);
        });

        $("#dateBeginOrder").click(function(){
            $(this).focus();
            $(this).select();
        });
        $("#dateEndOrder").click(function(){
            $(this).focus();
            $(this).select();
        });
        $("#dateBeginDeli").click(function(){
            $(this).focus();
            $(this).select();
        });
        $("#dateEndDeli").click(function(){
            $(this).focus();
            $(this).select();
        });
        $("#email").click(function(){
            $(this).focus();
            $(this).select();
        });
        $("#phone").click(function(){
            $(this).focus();
            $(this).select();
        });
    },

    InitDatePicker: function(){
        $("#dateBeginOrder").datetimepicker({
            dateFormat: 'yyyy/mm/dd'
        });

        $("#dateEndOrder").datetimepicker({
            dateFormat:'yyyy/mm/dd'
        });

        $("#dateBeginDeli").datetimepicker({
            dateFormat: 'yyyy/mm/dd'
        });

        $("#dateEndDeli").datetimepicker({
            dateFormat: 'yyyy/mm/dd'
        });
    },

    init:function(){
        $("ul.pager > li > a").click(function(){
            GetOrdersPager($(this).attr("page"));
        });

        MainScript.InitViewDetail();
        MainScript.InitDeleteOrder();
        MainScript.InitDialog();
        MainScript.InitMessagebox();
        MainScript.InitDatePicker();
        MainScript.InitSearch();
        MainScript.InitUpdateOrderState();
    }
}

function GetOrdersPager(page){
    $(".loading").css("width", $("#contentOrders").width());
    $(".loading").css("height", $("#contentOrders").height());
    $(".loading").css("display", "block");
    $.ajax({
        url:"../ajax/ajax.GetOrders.php?page="+page,
        type:"GET",
        dataType:"html",
        cache:false,
        success:function(data){
            $("#contentOrders").html(data);
            $("ul.pager > li > a").click(function(){
                GetOrdersPager($(this).attr("page"));
                $(".loading").css("display", "none");
            });
            MainScript.init();
        }
    });
}

function GetOrdersPagerByDateOrder(page){
    var dateBegin = $("#dateBeginOrder").val();
    var dateEnd = $("#dateBeginOrder").val();
    $(".loading").css("width", $("#contentOrders").width());
    $(".loading").css("height", $("#contentOrders").height());
    $(".loading").css("display", "block");
    $.ajax({
        url:"../ajax/ajax.SearchOrderByCondition.php",
        type:"POST",
        data: {
            "dateBeginOrder":dateBegin,
            "dateEndOrder":dateEnd,
            "page":page
        },
        dataType:"html",
        cache:false,
        success:function(data){
            $("#contentOrders").html(data);
            $("ul.pager > li > a").click(function(){
                GetOrdersPagerByDateOrder($(this).attr("page"));
                $(".loading").css("display", "none");
            });
            MainScript.init();
        }
    });
}

function GetOrdersPagerByDateDelivery(page){
    var dateBegin = $("#dateBeginDeli").val();
    var dateEnd = $("#dateEndDeli").val();
    $(".loading").css("width", $("#contentOrders").width());
    $(".loading").css("height", $("#contentOrders").height());
    $(".loading").css("display", "block");
    $.ajax({
        url:"../ajax/ajax.SearchOrderByCondition.php",
        type:"POST",
        data: {
            "dateBeginDeli":dateBegin,
            "dateEndDeli":dateEnd,
            "page":page
        },
        dataType:"html",
        cache:false,
        success:function(data){
            $("#contentOrders").html(data);
            $("ul.pager > li > a").click(function(){
                GetOrdersPagerByDateDelivery($(this).attr("page"));
                $(".loading").css("display", "none");
            });
            MainScript.init();
        }
    });
}

function GetOrdersPagerByCheck(page){
    var check=0;
    if($("#rdState").is(":checked")){
        check=1;
    }
    $(".loading").css("width", $("#contentOrders").width());
    $(".loading").css("height", $("#contentOrders").height());
    $(".loading").css("display", "block");
    $.ajax({
        url:"../ajax/ajax.SearchOrderByCondition.php",
        type:"POST",
        data: {
            "check":check,
            "page":page
        },
        dataType:"html",
        cache:false,
        success:function(data){
            $("#contentOrders").html(data);
            $("ul.pager > li > a").click(function(){
                GetOrdersPagerByCheck($(this).attr("page"));
                $(".loading").css("display", "none");
            });
            MainScript.init();
        }
    });
}

function GetOrdersPagerByEmail(page){
    var email=$("#email").val();
    $(".loading").css("width", $("#contentOrders").width());
    $(".loading").css("height", $("#contentOrders").height());
    $(".loading").css("display", "block");
    $.ajax({
        url:"../ajax/ajax.SearchOrderByCondition.php",
        type:"POST",
        data: {
            "email":email,
            "page":page
        },
        dataType:"html",
        cache:false,
        success:function(data){
            $("#contentOrders").html(data);
            $("ul.pager > li > a").click(function(){
                GetOrdersPagerByEmail($(this).attr("page"));
                $(".loading").css("display", "none");
            });
            MainScript.init();
        }
    });
}

function GetOrdersPagerByPhone(page){
    var phone=$("#phone").val();
    $(".loading").css("width", $("#contentOrders").width());
    $(".loading").css("height", $("#contentOrders").height());
    $(".loading").css("display", "block");
    $.ajax({
        url:"../ajax/ajax.SearchOrderByCondition.php",
        type:"POST",
        data: {
            "phone":phone,
            "page":page
        },
        dataType:"html",
        cache:false,
        success:function(data){
            $("#contentOrders").html(data);
            $("ul.pager > li > a").click(function(){
                GetOrdersPagerByPhone($(this).attr("page"));
                $(".loading").css("display", "none");
            });
            MainScript.init();
        }
    });
}

function GetOrderDetail(orderId){
    $(".loading").css("width", $("#contentOrders").width());
    $(".loading").css("height", $("#contentOrders").height());
    $(".loading").css("display", "block");
    $.ajax({
        url:"../ajax/ajax.dialog_OrderDetail.php",
        data: {
            "id": orderId
        },
        type:"POST",
        dataType:"html",
        cache:false,
        success:function(data){
            $("#dialog").html(data);
            $("#dialog").dialog('open');
            $(".loading").css("display", "none");
        }
    });
}

function DeleteOrder(orderId){
    $(".loading").css("width", $("#contentOrders").width());
    $(".loading").css("height", $("#contentOrders").height());
    $(".loading").css("display", "block");
    $.ajax({
        url:"../ajax/ajax.DeleteOrder.php",
        data: {
            "id": orderId
        },
        type:"POST",
        dataType:"html",
        success:function(data){
            if(data=="1"){
                $("#messagebox").dialog('close');

                $("#tr-"+ orderId).fadeOut('slow');
                $(".loading").css("display", "none");
            }else{
                $('#messagebox').html("Có lỗi xảy ra, vui lòng thử lại!");
                $("#messagebox").dialog('open');
                $('#messagebox').dialog({
                    buttons: {
                        "Đóng": function() {
                            $(this).dialog("close");
                            $(".loading").css("display", "none");
                        }
                    }
                })
            }
        }
    })
}

function UpdateOrderState(orderId,check){
    $(".loading").css("width", $("#contentOrders").width());
    $(".loading").css("height", $("#contentOrders").height());
    $(".loading").css("display", "block");
    $.ajax({
        url:"../ajax/ajax.UpdateOrderState.php",
        data: {
            "orderId": orderId,
            "checked":check
        },
        type:"POST",
        dataType:"html",
        success:function(data){
            if(data=="1"){
                if(check==1){
                    $("#tr-"+orderId).css("background-color", "#f0f7fa");
                }
                else{
                    $("#tr-"+orderId).css("background-color", "#ffffff");
                }
                $(".loading").css("display", "none");
            }else{
                $(".loading").css("display", "none");
                alert("Cập nhật đơn đặt hàng không thành công, vui lòng thử lại!");
                window.location='OrderManager.php';
            }
        }
    })
}

